package atm.design;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class ATMSystem {
    public static void main(String[] args) {
        ArrayList<Account> array = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        money:
        while (true) {
            System.out.println("=======欢迎使用ATM系统=======");
            System.out.println("1、登录");
            System.out.println("2、注册");
            System.out.println("3、退出");
            System.out.println("请回复数字进行操作：");
            int num = sc.nextInt();
            switch (num) {
                case 1:
                    //登录
                    enter(sc, array);
                    break;
                case 2:
                    //注册
                    array.add(register(array, sc));
                    break;
                case 3:
                    System.out.println("感谢您的使用，再见！");
                    break money;
                default:
                    System.out.println("输入有误，请重新输入：");
            }
        }
    }

    private static void enter(Scanner sc, ArrayList<Account> array) {
        if (array.size() == 0) {
            System.out.println("账号为空，请先注册再登录");
            return;
        }
        while (true) {
            System.out.println("请输入卡号：");
            String cardId = sc.next();
            System.out.println("请输入密码：");
            String pass = sc.next();
            for (int i = 0; i < array.size(); i++) {
                if (cardId.equals(array.get(i).getCardId()) && pass.equals(array.get(i).getPassWord())) {
                    System.out.println("登录成功");
                    upload(sc, array, cardId);
                    return;
                }
            }
            System.out.println("输入有误，请重新输入：");
        }

    }

    //登录后的操作
    private static void upload(Scanner sc, ArrayList<Account> array, String cardId) {
        while (true) {
            System.out.println("请回复数字进行操作：");
            System.out.println("1、查询");
            System.out.println("2、存款");
            System.out.println("3、取款");
            System.out.println("4、转账");
            System.out.println("5、修改密码");
            System.out.println("6、退出");
            System.out.println("7、注销账户");
            int nu = sc.nextInt();
            switch (nu) {
                case 1://查询
                    query(array, cardId);
                    break;
                case 2://存款
                    deposit(array, sc, cardId);
                    System.out.println("存款成功");
                    break;
                case 3://取款
                    withdrawal(array, sc, cardId);
                    System.out.println("取款成功");
                    break;
                case 4://转账
                    transfer(array, sc, cardId);
                    System.out.println("转账成功");
                    break;
                case 5://修改密码
                    amend(array, sc, cardId);
                    System.out.println("修改成功，请重新登录");
                    return;
                case 6://退出
                    System.out.println("感谢您的使用！");
                    return;
                case 7://注销账户
                    cancel(array, cardId);
                    System.out.println("注销成功，感谢您的使用！");
                    return;
                default:
                    System.out.println("输入有误，请重新输入：");
                    break;
            }
        }
    }

    //注销账户
    private static void cancel(ArrayList<Account> array, String cardId) {
        for (int i = 0; i < array.size(); i++) {
            if (cardId.equals(array.get(i).getCardId())) {
                array.remove(i);
            }
        }
    }

    //修改密码的方法
    private static void amend(ArrayList<Account> array, Scanner sc, String cardId) {
        for (int i = 0; i < array.size(); i++) {
            if (array.get(i).getCardId().equals(cardId)) {
                while (true) {
                    System.out.println("请输入需要修改的密码：");
                    String pass = sc.next();
                    System.out.println("请再次输入密码：");
                    String pass1 = sc.next();
                    if (pass.equals(pass1)) {
                        array.get(i).setPassWord(pass);
                        return;
                    }
                    System.out.println("两次输入不一致，请重新输入：");
                }
            }
        }
    }

    //转账方法
    private static void transfer(ArrayList<Account> array, Scanner sc, String cardId) {
        while (true) {
            System.out.println("请输入转账的账号：");
            String number = sc.next();
            for (int i = 0; i < array.size(); i++) {
                if (number.equals(array.get(i).getCardId()) && affirm(array, sc, number)) {
                    for (int j = 0; j < array.size(); j++) {
                        if (cardId.equals(array.get(j).getCardId())) {
                            while (true) {
                                System.out.println("请输入需要转账金额：");
                                double money = sc.nextDouble();
                                if (money > array.get(j).getSurplus() && money <= array.get(j).getLimit()) {
                                    System.out.println("转账金额超过余额，请再次输入：");
                                } else if (money > array.get(j).getLimit()) {
                                    System.out.println("超过当日限额，请重新输入：");
                                } else {
                                    double surplus = array.get(j).getSurplus() - money;
                                    array.get(j).setSurplus(surplus);
                                    double sur = array.get(i).getSurplus() + money;
                                    array.get(i).setSurplus(sur);
                                    return;
                                }
                            }
                        }
                    }
                }
            }
            System.out.println("输入的账号有误，请重新输入");
        }
    }

    //输入名字的姓判断是否正确
    private static boolean affirm(ArrayList<Account> array, Scanner sc, String number) {
        for (int i = 0; i < array.size(); i++) {
            if (number.equals(array.get(i).getCardId())) {
                String name = array.get(i).getName();
                char c = name.charAt(0);
                String str = "";
                str += c;
                String nameNext = name.replaceAll(str, "*");
                while (true) {
                    System.out.println("请将*位置的字打出来：" + nameNext);
                    String nameFirst = sc.next();
                    if (nameFirst.equals(str)) {
                        System.out.println("输入正确");
                        return true;
                    } else {
                        System.out.println("输入有误");
                    }
                }
            }
        }
        return false;
    }

    //取款方法
    private static void withdrawal(ArrayList<Account> array, Scanner sc, String cardId) {
        for (int i = 0; i < array.size(); i++) {
            if (cardId.equals(array.get(i).getCardId())) {
                while (true) {
                    System.out.println("请输入取款数：");
                    double money = sc.nextDouble();
                    if (money > array.get(i).getSurplus() && money <= array.get(i).getLimit()) {
                        System.out.println("存款不足，请确认后再提取；");
                    } else if (money > array.get(i).getLimit()) {
                        System.out.println("超过当日限额，请重新输入：");
                    } else {
                        double mon = array.get(i).getSurplus() - money;
                        array.get(i).setSurplus(mon);
                        return;
                    }
                }
            }
        }
    }

    //存款方法
    private static void deposit(ArrayList<Account> array, Scanner sc, String cardId) {
        System.out.println("请输入需要存款数：");
        double money = sc.nextDouble();
        for (int j = 0; j < array.size(); j++) {
            if (cardId.equals(array.get(j).getCardId())) {
                double count = array.get(j).getSurplus() + money;
                array.get(j).setSurplus(count);
            }
        }

    }

    //查询方法
    private static void query(ArrayList<Account> array, String cardId) {
        for (int j = 0; j < array.size(); j++) {
            if (cardId.equals(array.get(j).getCardId())) {
                System.out.println("卡号：" + cardId);
                System.out.println("余额：" + array.get(j).getSurplus());
                System.out.println("限额：" + array.get(j).getLimit());
            }
        }
    }

    //注册方法
    private static Account register(ArrayList<Account> array, Scanner sc) {
        String cardID = "";
        double surplus = 0.0;
        while (true) {
            System.out.println("请输入姓名：");
            String name = sc.next();
            System.out.println("请输入密码：");
            String password = sc.next();
            System.out.println("请再次输入密码：");
            String password01 = sc.next();
            if (password.equals(password01)) {
                cardID += show(array);
                System.out.println("请输入取款限额");
                int limit = sc.nextInt();
                Account account = new Account(name, cardID, password, surplus, limit);
                System.out.println("注册成功，您的卡号是" + cardID);
                return account;
            } else {
                System.out.println("密码输入有误，请重新输入：");
            }
        }
    }

    //随机产生8位数字的卡号的方法
    private static String show(ArrayList<Account> account) {
        Random r = new Random();

        asd:
        while (true) {
            String card = "";
            for (int i = 0; i < 8; i++) {
                int num = r.nextInt(10);
                card += num;
            }
            String cd = showelse(account, card);
            if (cd == null) {
                return card;
            }
        }
    }

    //确定卡号不会重复的方法
    private static String showelse(ArrayList<Account> account, String card) {
        for (int i = 0; i < account.size(); i++) {
            if (card.equals(account.get(i).getCardId())) {
                return card;
            }
        }
        return null;
    }
}
